<?php
include("./config/session-action.php");
include("./config/config.inc.php");
include("./config/QuickMySQL.php");
include("./config/function.inc.php");

/*发送邮件处理类*/
class CSendEMailGroupAction extends JsonBase
{
	/* 删除队列 */
	function OnDelete(){
		if(!isset($_POST['id'])){
			$this->json["msg"] = "参数错误";
			return;
		}
		$id = (int)$_POST['id'];
		/*$sql = "delete from t_sendemail_group where id={$id}";
		if( QuickMySQL::quickExecute($sql) ){
			$this->json["success"] = true;
			$this->json["msg"] = "删除ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "删除失败";
		}*/
		$mysql = new QuickMySQL();
		$sql = "select sm_to,sm_flag from t_sendemail_group where id={$id}";
		$res = $mysql->Query($sql);
		if( !($row = mysql_fetch_row($res)) )
			return;
		$sm_to = $row[0];
		$sm_flag = 0;
		if( (int)$row[1] < 32 && (int)$row[1] > 0)
			$sm_flag = 1 << (int)$row[1];
		mysql_free_result($res);
		// 开始删除
		$mysql->Begin();
		$mysql->Query("delete from t_sendemail_group where id={$id}");
		$mysql->Query("update {$sm_to} set sem_n = sem_n & ~{$sm_flag}");
		if( $mysql->Commit() ){
			$this->json["success"] = true;
			$this->json["msg"] = "删除ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "删除失败";
		}
	}
	/* 启动队列 */
	function OnStart(){
		if(!isset($_POST['id'])){
			$this->json["msg"] = "参数错误";
			return;
		}
		$id = (int)$_POST['id'];
		$sql = "update t_sendemail_group set sm_run=1 where id={$id}";
		if( QuickMySQL::quickExecute($sql) ){
			$this->json["success"] = true;
			$this->json["msg"] = "启动ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "启动失败";
		}		
	}
	/* 暂停队列 */
	function OnPause(){
		if(!isset($_POST['id'])){
			$this->json["msg"] = "参数错误";
			return;
		}
		$id = (int)$_POST['id'];
		$sql = "update t_sendemail_group set sm_run=0 where id={$id}";
		if( QuickMySQL::quickExecute($sql) ){
			$this->json["success"] = true;
			$this->json["msg"] = "暂停ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "暂停失败";
		}		
	}
	/* 停止队列 */
	function OnStop(){
		if(!isset($_POST['id'])){
			$this->json["msg"] = "参数错误";
			return;
		}
		$id = (int)$_POST['id'];
		/*$sql = "update t_sendemail_group set sm_run=0, sm_flag=id+32 where id={$id}";
		if( QuickMySQL::quickExecute($sql) ){
			$this->json["success"] = true;
			$this->json["msg"] = "停止ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "停止失败";
		}*/
		$mysql = new QuickMySQL();
		$sql = "select sm_to,sm_flag from t_sendemail_group where id={$id}";
		$res = $mysql->Query($sql);
		if( !($row = mysql_fetch_row($res)) )
			return;
		$sm_to = $row[0];
		$sm_flag = 0;
		if( (int)$row[1] < 32 && (int)$row[1] > 0)
			$sm_flag = 1 << (int)$row[1];
		mysql_free_result($res);
		// 开始删除
		$mysql->Begin();
		$mysql->Query("update t_sendemail_group set sm_run=0, sm_flag=id+32 where id={$id}");
		$mysql->Query("update {$sm_to} set sem_n = sem_n & ~{$sm_flag}");
		if( $mysql->Commit() ){
			$this->json["success"] = true;
			$this->json["msg"] = "停止ID为{$id}的群发队列成功";
		} else {
			$this->json["msg"] = "停止失败";
		}		
	}
}

set_time_limit(0);
/*发送邮件操作*/
$cc = new CSendEMailGroupAction();

?>